﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using BBMS.DLL.Base;
using BBMS.DLL.DataContract;

namespace BBMS.DLL.Forms
{
    public partial class frmAuthorizationUpdate : Form
    {
        private Hashtable functionRoleList = new Hashtable();
//         public new static readonly string FunctionKey = "PhanQuyen";
        protected static string GetFunctionKey() { return "PhanQuyen"; }
        public frmAuthorizationUpdate()
        {
            InitializeComponent();
           /* User user1 = new User(0, "admin123");
            KeyValuePair kvp1 = new KeyValuePair(user1, user1.UserName);
            User user2 = new User(1, "baonv");
            KeyValuePair kvp2 = new KeyValuePair(user2, user2.UserName);
            User user3 = new User(2, "toanhm");
            KeyValuePair kvp3 = new KeyValuePair(user3, user3.UserName);
            cbUser.Items.Add(kvp1);
            cbUser.Items.Add(kvp2);
            cbUser.Items.Add(kvp3);*/
            GetUserList();

            InitRole();
        }

        private void GetUserList()
        {
            System.Collections.ArrayList ar = NhanVienList.getAllUserName();
            for (int i = 0; i < ar.Count; i++)
            {
                KeyValuePair kvp = (KeyValuePair)ar[i];
                cbUser.Items.Add(kvp);
            }
            cbUser.SelectedIndex = 0;
        }

        private void InitRole()
        {
            foreach (NhomChucNang item in Enum.GetValues(typeof(NhomChucNang)))
            {
                TreeNode tn = new TreeNode();
                tn.Name = item.ToString();
                tn.Text = StringEnum.GetStringValue(item);
                foreach (DictionaryEntry obj in Config.DSNhomChucNang)
                {
                    ChucNang cn = (ChucNang)obj.Value;
                    if (cn.Nhom == item.ToString())
                    {
                        TreeNode childNode = new TreeNode();
                        childNode.Name = cn.Ma;
                        childNode.Text = cn.Ten;
                        tn.Nodes.Add(childNode);
                    }

                }

                tvRole.Nodes.Add(tn);
            }
         
            //foreach (Category item in Enum.GetValues(typeof(Category)))
            //{
            //    cbCategory.Items.Add(new KeyValuePair((int)item, StringEnum.GetStringValue(item)));
            //}
            //foreach (DictionaryEntry obj in Config.DSNhomChucNang)
            //{
            //   FunctionRole fr = (FunctionRole)obj.Value;
            //  // TreeNode tn = new TreeNode(fr.Function);
            //   TreeNode tn = new TreeNode();
            //   tn.Name = "mainNode";
            //   tn.Text = "Main";
            //}
        }

        private void tvRole_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            TreeNode tn = e.Node;
            tvRole.SelectedNode = e.Node;
            if (tn != null && tn.Parent == null)
            {
                //chkRead.Checked = false;
                //chkRead.ThreeState = false;
               // chkRead.ThreeState = true;
                
                bool haveCheckRead = false;
                bool haveCheckReadAll = true;
                bool haveCheckAdd = false;
                bool haveCheckAddAll = true;
                bool haveCheckEdit = false;
                bool haveCheckEditAll = true;
                bool haveCheckDelete = false;
                bool haveCheckDeleteAll = true;
                foreach (DictionaryEntry obj in functionRoleList)
                {
                    bool existRoleRead = false;
                    bool existRoleAdd = false;
                    bool existRoleEdit = false;
                    bool existRoleDelete = false;

                    for (int i = 0; i < tn.Nodes.Count; i++)
                    {
                        if (obj.Key.ToString() == tn.Nodes[i].Name)
                        {
                            string value = obj.Value.ToString();
                            if (value[0] == '1')
                            {
                                haveCheckRead = true;
                                existRoleRead = true;
                            }
                            else haveCheckReadAll = false;
                            if (value[1] == '1')
                            {
                                haveCheckAdd = true;
                                existRoleAdd = true;
                            }
                            else haveCheckAddAll = false;
                            if (value[2] == '1')
                            {
                                haveCheckEdit = true;
                                existRoleEdit = true;
                            }
                            else haveCheckEditAll = false;
                            if (value[3] == '1')
                            {
                                haveCheckDelete = true;
                                existRoleDelete = true;
                            }
                            else haveCheckDeleteAll = false;

                        }
                    }
                  //  if (!existRoleRead) haveCheckAll = false;
                }

                if (haveCheckRead && haveCheckReadAll) chkRead.CheckState = CheckState.Checked;
                if (haveCheckRead && !haveCheckReadAll) chkRead.CheckState = CheckState.Indeterminate;
                if (!haveCheckRead) chkRead.CheckState = CheckState.Unchecked;
                if (haveCheckAdd && haveCheckAddAll) chkAdd.CheckState = CheckState.Checked;
                if (haveCheckAdd && !haveCheckAddAll) chkAdd.CheckState = CheckState.Indeterminate;
                if (!haveCheckAdd) chkAdd.CheckState = CheckState.Unchecked;
                if (haveCheckEdit && haveCheckEditAll) chkEdit.CheckState = CheckState.Checked;
                if (haveCheckEdit && !haveCheckEditAll) chkEdit.CheckState = CheckState.Indeterminate;
                if (!haveCheckEdit) chkEdit.CheckState = CheckState.Unchecked;
                if (haveCheckDelete && haveCheckDeleteAll) chkDelete.CheckState = CheckState.Checked;
                if (haveCheckDelete && !haveCheckDeleteAll) chkDelete.CheckState = CheckState.Indeterminate;
                if (!haveCheckDelete) chkDelete.CheckState = CheckState.Unchecked;

            }
          //  ResetCheckRole();
            if (tn.Parent != null && tn != null)
            {
                ResetCheckRole();
                foreach (DictionaryEntry obj in functionRoleList)
                {
                    if (obj.Key.ToString() == tn.Name)
                    {
                        string value = obj.Value.ToString();
                        if (value[0] == '1') chkRead.CheckState = CheckState.Checked;//chkRead.Checked = true;
                        else chkRead.CheckState = CheckState.Unchecked;//chkRead.Checked = false;
                        if (value[1] == '1') chkAdd.CheckState = CheckState.Checked;//chkAdd.Checked = true;
                        else chkAdd.CheckState = CheckState.Unchecked;//chkAdd.Checked = false;
                        if (value[2] == '1') chkEdit.CheckState = CheckState.Checked;//chkEdit.Checked = true;
                        else chkEdit.CheckState = CheckState.Unchecked; //chkEdit.Checked = false;
                        if (value[3] == '1') chkDelete.CheckState = CheckState.Checked;//chkDelete.Checked = true;
                        else chkDelete.CheckState = CheckState.Unchecked;//chkDelete.Checked = false;
                        break;
                    }
                }
               /* KeyValuePair kvp = (KeyValuePair)cbUser.SelectedItem;
                User user = (User)kvp.Key;
                if (user.UserRoles.Count > 0)
                {
                    for (int i = 0; i < user.UserRoles.Count; i++)
                    {
                        if (user.HaveRole(Role.Read,tn.Name))
                        {
                            chkRead.Checked = true;
                        }
                        if (user.HaveRole(Role.Add, tn.Name))
                        {
                            chkAdd.Checked = true;
                        }
                        if (user.HaveRole(Role.Edit, tn.Name))
                        {
                            chkEdit.Checked = true;
                        }
                        if (user.HaveRole(Role.Delete, tn.Name))
                        {
                            chkDelete.Checked = true;
                        }

                    }
                }*/
            }
        }

        private void ResetCheckRole()
        {
            chkRead.CheckState = CheckState.Unchecked;
            chkAdd.CheckState = CheckState.Unchecked;
            //chkAdd.Checked = false;
            chkEdit.CheckState = CheckState.Unchecked;
            chkDelete.CheckState = CheckState.Unchecked;
           /* chkAdd.Checked = false;
            chkDelete.Checked = false;
            chkEdit.Checked = false;
            chkRead.Checked = false;*/
        }

        private void ClearUserInfo()
        {
            txtFullName.Text = "";
            txtMobile.Text = "";
            txtDepartment.Text = "";
            txtPosition.Text = "";
        }

        private void cbUser_SelectedIndexChanged(object sender, EventArgs e)
        {
            tvRole.SelectedNode = null;
            ResetCheckRole();
            functionRoleList = new Hashtable();
            KeyValuePair kvp = (KeyValuePair)cbUser.SelectedItem;
            ClearUserInfo();
            NhanVien nv = (NhanVien)kvp.Key;
            txtFullName.Text = nv.FullName;
            txtMobile.Text = nv.Mobile;
            txtDepartment.Text = nv.Department;
            txtPosition.Text = nv.Position;
            User user = new User(nv.ID, kvp.Value.ToString());
            functionRoleList = user.UserRoles;
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            KeyValuePair kvp = (KeyValuePair)cbUser.SelectedItem;
            string msg = NhanVienQuyen.InsertRole(((NhanVien)(kvp.Key)).ID, functionRoleList);
            if (msg == "")
            {
                MessageBox.Show("Thiết lập quyền cho người dùng thành công");
               // ResetCheckRole();
               // functionRoleList = new Hashtable();                
               // User user = new User(Convert.ToInt32(kvp.Key), kvp.Value.ToString());
                //functionRoleList = user.UserRoles;
            }
            else
            {
                MessageBox.Show(msg);
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        //private void chkRead_CheckedChanged(object sender, EventArgs e)
        //{
        //    TreeNode tn = tvRole.SelectedNode;
        //   /*if (tn != null && tn.Parent == null)
        //    {
        //        for (int i = 0; i < tn.Nodes.Count; i++)
        //        {
        //            bool findRole = false;
        //            foreach (DictionaryEntry obj in functionRoleList)
        //            {
        //                if (obj.Key.ToString() == tn.Nodes[i].Name)
        //                {
        //                    string value = obj.Value.ToString();
        //                    functionRoleList.Remove(obj.Key);
        //                    if (chkRead.Checked) functionRoleList.Add(tn.Nodes[i].Name, "1" + value.Substring(1));
        //                    else functionRoleList.Add(tn.Nodes[i].Name, "0" + value.Substring(1));
        //                    findRole = true;
        //                    break;
        //                }
        //            }
        //            if (!findRole)
        //            {
        //                if (chkRead.Checked) functionRoleList.Add(tn.Nodes[i].Name, "1000");
        //                else functionRoleList.Add(tn.Nodes[i].Name, "0000");
        //            }
        //        }
              
        //    }*/
        //    if (tn != null && tn.Parent != null)
        //    {
        //        bool findRole = false;
        //        foreach (DictionaryEntry obj in functionRoleList)
        //        {                    
        //            if (obj.Key.ToString() == tn.Name)
        //            {
        //                string value = obj.Value.ToString();
        //                functionRoleList.Remove(obj.Key);
        //                if (chkRead.Checked) functionRoleList.Add(tn.Name, "1" + value.Substring(1));
        //                else functionRoleList.Add(tn.Name, "0" + value.Substring(1));
        //                findRole = true;
        //                break;
        //            }
        //        }
        //        if (!findRole)
        //        {
        //            if (chkRead.Checked) functionRoleList.Add(tn.Name, "1000");
        //            else functionRoleList.Add(tn.Name, "0000");
        //        }

        //    }
        //}

        //private void chkAdd_CheckedChanged(object sender, EventArgs e)
        //{
        //    /*Point point = Control.MousePosition;
        //    MouseButtons mousebutton = Control.MouseButtons;
        //    if (chkAdd.AutoCheck)
        //    {
        //        int a = 0;
        //    }*/
        //    TreeNode tn = tvRole.SelectedNode;
        //    if (tn != null && tn.Parent != null)
        //    {
        //        bool findRole = false;
        //        foreach (DictionaryEntry obj in functionRoleList)
        //        {
        //            if (obj.Key.ToString() == tn.Name)
        //            {
        //                string value = obj.Value.ToString();
        //                functionRoleList.Remove(obj.Key);
        //                if (chkAdd.Checked) functionRoleList.Add(tn.Name, value.Substring(0, 1) + "1" + value.Substring(2));
        //                else functionRoleList.Add(tn.Name, value.Substring(0, 1) + "0" + value.Substring(2));
        //                findRole = true;
        //                break;
        //            }
        //        }
        //        if (!findRole)
        //        {
        //            if (chkAdd.Checked) functionRoleList.Add(tn.Name, "0100");
        //            else functionRoleList.Add(tn.Name, "0000");
        //        }

        //    }
        //}

        //private void chkEdit_CheckedChanged(object sender, EventArgs e)
        //{
        //    TreeNode tn = tvRole.SelectedNode;
        //    if (tn != null && tn.Parent != null)
        //    {
        //        bool findRole = false;
        //        foreach (DictionaryEntry obj in functionRoleList)
        //        {
        //            if (obj.Key.ToString() == tn.Name)
        //            {
        //                string value = obj.Value.ToString();
        //                functionRoleList.Remove(obj.Key);
        //                if (chkEdit.Checked) functionRoleList.Add(tn.Name, value.Substring(0, 2) + "1" + value.Substring(3));
        //                else functionRoleList.Add(tn.Name, value.Substring(0, 2) + "0" + value.Substring(3));
        //                findRole = true;
        //                break;
        //            }
        //        }
        //        if (!findRole)
        //        {
        //            if (chkEdit.Checked) functionRoleList.Add(tn.Name, "0010");
        //            else functionRoleList.Add(tn.Name, "0000");
        //        }

        //    }
        //}

        //private void chkDelete_CheckedChanged(object sender, EventArgs e)
        //{
        //    TreeNode tn = tvRole.SelectedNode;
        //    if (tn != null && tn.Parent != null)
        //    {
        //        bool findRole = false;
        //        foreach (DictionaryEntry obj in functionRoleList)
        //        {
        //            if (obj.Key.ToString() == tn.Name)
        //            {
        //                string value = obj.Value.ToString();
        //                functionRoleList.Remove(obj.Key);
        //                if (chkDelete.Checked) functionRoleList.Add(tn.Name, value.Substring(0, 3) + "1");
        //                else functionRoleList.Add(tn.Name, value.Substring(0, 3) + "0");
        //                findRole = true;
        //                break;
        //            }
        //        }
        //        if (!findRole)
        //        {
        //            if (chkDelete.Checked) functionRoleList.Add(tn.Name, "0001");
        //            else functionRoleList.Add(tn.Name, "0000");
        //        }
        //    }
        //}

        private void chkAdd_Click(object sender, EventArgs e)
        {
            //chkAdd.Checked = !chkAdd.Checked;
            if (chkAdd.Checked)
            {
                chkAdd.Checked = false;
                
            }
            else
            {
                chkAdd.Checked = true;
            }

            TreeNode tn = tvRole.SelectedNode;
            if (tn != null && tn.Parent != null)
            {
                bool findRole = false;
                foreach (DictionaryEntry obj in functionRoleList)
                {
                    if (obj.Key.ToString() == tn.Name)
                    {
                        string value = obj.Value.ToString();
                        functionRoleList.Remove(obj.Key);
                        if (chkAdd.Checked) functionRoleList.Add(tn.Name, value.Substring(0, 1) + "1" + value.Substring(2));
                        else functionRoleList.Add(tn.Name, value.Substring(0, 1) + "0" + value.Substring(2));
                        findRole = true;
                        break;
                    }
                }
                if (!findRole)
                {
                    if (chkAdd.Checked) functionRoleList.Add(tn.Name, "0100");
                    else functionRoleList.Add(tn.Name, "0000");
                }

            }
            if (tn != null && tn.Parent == null)
            {
                for (int i = 0; i < tn.Nodes.Count; i++)
                {
                    bool findRole = false;
                    foreach (DictionaryEntry obj in functionRoleList)
                    {
                        if (obj.Key.ToString() == tn.Nodes[i].Name)
                        {
                            string value = obj.Value.ToString();
                            functionRoleList.Remove(obj.Key);
                            if (chkAdd.Checked) functionRoleList.Add(tn.Nodes[i].Name, value.Substring(0, 1) + "1" + value.Substring(2));
                            else functionRoleList.Add(tn.Nodes[i].Name, value.Substring(0, 1) + "0" + value.Substring(2));
                            findRole = true;
                            break;
                        }
                    }
                    if (!findRole)
                    {
                        if (chkAdd.Checked) functionRoleList.Add(tn.Nodes[i].Name, "0100");
                        else functionRoleList.Add(tn.Nodes[i].Name, "0000");
                    }
                }
            }
        }

        private void chkRead_Click(object sender, EventArgs e)
        {
            if (chkRead.Checked)
            {
                chkRead.Checked = false;

            }
            else
            {
                chkRead.Checked = true;
            }
            TreeNode tn = tvRole.SelectedNode;
            if (tn != null && tn.Parent != null)
            {
                bool findRole = false;
                foreach (DictionaryEntry obj in functionRoleList)
                {
                    if (obj.Key.ToString() == tn.Name)
                    {
                        string value = obj.Value.ToString();
                        functionRoleList.Remove(obj.Key);
                        if (chkRead.Checked) functionRoleList.Add(tn.Name, "1" + value.Substring(1));
                        else functionRoleList.Add(tn.Name, "0" + value.Substring(1));
                        findRole = true;
                        break;
                    }
                }
                if (!findRole)
                {
                    if (chkRead.Checked) functionRoleList.Add(tn.Name, "1000");
                    else functionRoleList.Add(tn.Name, "0000");
                }

            }
            if (tn != null && tn.Parent == null)
            {
                for (int i = 0; i < tn.Nodes.Count; i++)
                {
                    bool findRole = false;
                    foreach (DictionaryEntry obj in functionRoleList)
                    {
                        if (obj.Key.ToString() == tn.Nodes[i].Name)
                        {
                            string value = obj.Value.ToString();
                            functionRoleList.Remove(obj.Key);
                            if (chkRead.Checked) functionRoleList.Add(tn.Nodes[i].Name, "1" + value.Substring(1));
                            else functionRoleList.Add(tn.Nodes[i].Name, "0" + value.Substring(1));
                            findRole = true;
                            break;
                        }
                    }
                    if (!findRole)
                    {
                        if (chkRead.Checked) functionRoleList.Add(tn.Nodes[i].Name, "1000");
                        else functionRoleList.Add(tn.Nodes[i].Name, "0000");
                    }
                }
            }
        }

        private void chkEdit_Click(object sender, EventArgs e)
        {
            if (chkEdit.Checked)
            {
                chkEdit.Checked = false;

            }
            else
            {
                chkEdit.Checked = true;
            }
            TreeNode tn = tvRole.SelectedNode;
            if (tn != null && tn.Parent != null)
            {
                bool findRole = false;
                foreach (DictionaryEntry obj in functionRoleList)
                {
                    if (obj.Key.ToString() == tn.Name)
                    {
                        string value = obj.Value.ToString();
                        functionRoleList.Remove(obj.Key);
                        if (chkEdit.Checked) functionRoleList.Add(tn.Name, value.Substring(0, 2) + "1" + value.Substring(3));
                        else functionRoleList.Add(tn.Name, value.Substring(0, 2) + "0" + value.Substring(3));
                        findRole = true;
                        break;
                    }
                }
                if (!findRole)
                {
                    if (chkEdit.Checked) functionRoleList.Add(tn.Name, "0010");
                    else functionRoleList.Add(tn.Name, "0000");
                }

            }

            if (tn != null && tn.Parent == null)
            {
                for (int i = 0; i < tn.Nodes.Count; i++)
                {
                    bool findRole = false;
                    foreach (DictionaryEntry obj in functionRoleList)
                    {
                        if (obj.Key.ToString() == tn.Nodes[i].Name)
                        {
                            string value = obj.Value.ToString();
                            functionRoleList.Remove(obj.Key);
                            if (chkEdit.Checked) functionRoleList.Add(tn.Nodes[i].Name, value.Substring(0, 2) + "1" + value.Substring(3));
                            else functionRoleList.Add(tn.Nodes[i].Name, value.Substring(0, 2) + "0" + value.Substring(3));
                            findRole = true;
                            break;
                        }
                    }
                    if (!findRole)
                    {
                        if (chkEdit.Checked) functionRoleList.Add(tn.Nodes[i].Name, "0010");
                        else functionRoleList.Add(tn.Nodes[i].Name, "0000");
                    }
                }
            }


        }

        private void chkDelete_Click(object sender, EventArgs e)
        {
            if (chkDelete.Checked)
            {
                chkDelete.Checked = false;

            }
            else
            {
                chkDelete.Checked = true;
            }
            TreeNode tn = tvRole.SelectedNode;
            if (tn != null && tn.Parent != null)
            {
                bool findRole = false;
                foreach (DictionaryEntry obj in functionRoleList)
                {
                    if (obj.Key.ToString() == tn.Name)
                    {
                        string value = obj.Value.ToString();
                        functionRoleList.Remove(obj.Key);
                        if (chkDelete.Checked) functionRoleList.Add(tn.Name, value.Substring(0, 3) + "1");
                        else functionRoleList.Add(tn.Name, value.Substring(0, 3) + "0");
                        findRole = true;
                        break;
                    }
                }
                if (!findRole)
                {
                    if (chkDelete.Checked) functionRoleList.Add(tn.Name, "0001");
                    else functionRoleList.Add(tn.Name, "0000");
                }
            }
            if (tn != null && tn.Parent == null)
            {
                for (int i = 0; i < tn.Nodes.Count; i++)
                {
                    bool findRole = false;
                    foreach (DictionaryEntry obj in functionRoleList)
                    {
                        if (obj.Key.ToString() == tn.Nodes[i].Name)
                        {
                            string value = obj.Value.ToString();
                            functionRoleList.Remove(obj.Key);
                            if (chkDelete.Checked) functionRoleList.Add(tn.Nodes[i].Name, value.Substring(0, 3) + "1");
                            else functionRoleList.Add(tn.Nodes[i].Name, value.Substring(0, 3) + "0");
                            findRole = true;
                            break;
                        }
                    }
                    if (!findRole)
                    {
                        if (chkDelete.Checked) functionRoleList.Add(tn.Nodes[i].Name, "0001");
                        else functionRoleList.Add(tn.Nodes[i].Name, "0000");
                    }
                }
            }
        }

     
    }
}
